#include "picshow.h"
#include "ui_picshow.h"

PicShow::PicShow(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::PicShow)
{
    ui->setupUi(this);
    ui->preButton->SetIcons(":/icon/previous.png",":/icon/previous_hover.png",":/icon/previous_press.png");
    ui->nextButton->SetIcons(":/icon/next.png",":/icon/next_hover.png",":/icon/next_press.png");
    //将两侧的按钮设置为透明的
    QGraphicsOpacityEffect* opacity_pre = new QGraphicsOpacityEffect(this);
    opacity_pre->setOpacity(0);
    //后续修改 opacity_pre 的透明度值会直接影响 preButton 按钮的透明度。
    //因为 QGraphicsOpacityEffect 是一个实时应用的图形效果，
    //任何对其属性（比如 setOpacity）的修改，都会立刻反映在绑定的控件上。
    //所以，改变 opacity_pre 的值会影响到按钮的透明度，无论是通过动画还是直接修改透明度值。
    ui->preButton->setGraphicsEffect(opacity_pre);
    QGraphicsOpacityEffect* opacity_next = new QGraphicsOpacityEffect(this);
    opacity_next->setOpacity(0);
    ui->nextButton->setGraphicsEffect(opacity_next);

    _animation_show_pre = new QPropertyAnimation(opacity_pre,"opacity",this);
    _animation_show_pre->setEasingCurve(QEasingCurve::Linear);//设置动画的缓动曲线为线性
    _animation_show_pre->setDuration(500);//设置动画的持续时间为 500 毫秒
    _animation_show_next = new QPropertyAnimation(opacity_next,"opacity",this);
    _animation_show_next->setEasingCurve(QEasingCurve::Linear);
    _animation_show_next->setDuration(500);

    //点击向左向右按钮的时候发出信号，然后layout收到信号，然后layout再发出一个信号让protreeWidget收到信号实现联动效果
    connect(ui->preButton,&QPushButton::clicked,this,&PicShow::SigPreClicked);
    connect(ui->nextButton,&QPushButton::clicked,this,&PicShow::SigNextClicked);
}

PicShow::~PicShow()
{
    delete ui;
}

void PicShow::ReloadPic()//执行重新加载逻辑
{
    if(_selected_path != ""){
        const auto& width = ui->gridLayout->geometry().width();
        const auto& height = ui->gridLayout->geometry().height();
        _pix_map.load(_selected_path);
        _pix_map = _pix_map.scaled(width,height,Qt::KeepAspectRatio);//将图片拉伸
        ui->label->setPixmap(_pix_map);
    }
}

bool PicShow::event(QEvent *event)
{
    auto type = event->type();
    if(type == QEvent::Enter){//当鼠标进入区域时就执行显现逻辑
        ShowPreNextBtns(true);
    }else if(type == QEvent::Leave){//当鼠标离开区域时执行隐藏逻辑
        ShowPreNextBtns(false);
    }
    return QWidget::event(event);
}

void PicShow::ShowPreNextBtns(bool b_show)
{
    if(!b_show && _b_btnvisible){//将显示状态变为隐藏状态
        _animation_show_pre->stop();
        _animation_show_pre->setStartValue(1);//这里设置的就是opacity_pre对象的opacity属性
        _animation_show_pre->setEndValue(0);
        _animation_show_pre->start();

        _animation_show_next->stop();
        _animation_show_next->setStartValue(1);
        _animation_show_next->setEndValue(0);
        _animation_show_next->start();

        _b_btnvisible = false;
        return;
    }
    if(b_show && !_b_btnvisible){//将隐藏状态变为显示状态
        _animation_show_pre->stop();
        _animation_show_pre->setStartValue(0);
        _animation_show_pre->setEndValue(1);
        _animation_show_pre->start();

        _animation_show_next->stop();
        _animation_show_next->setStartValue(0);
        _animation_show_next->setEndValue(1);
        _animation_show_next->start();

        _b_btnvisible = true;
        return;
    }
}

void PicShow::SlotSelectItem(const QString &path)
{
    _selected_path = path;
    _pix_map.load(path);
    auto width = this->width()-20;
    auto height = this->height()-20;
    _pix_map = _pix_map.scaled(width,height,Qt::KeepAspectRatio);
    ui->label->setPixmap(_pix_map);//将图片放到右侧显示区域
}

void PicShow::SlotUpdatePic(const QString &path)
{
    _selected_path = path;
    _pix_map.load(path);
    auto width = this->width()-20;
    auto height = this->height()-20;
    _pix_map = _pix_map.scaled(width,height,Qt::KeepAspectRatio);
    ui->label->setPixmap(_pix_map);//将图片放到右侧显示区域
}

void PicShow::SlotDeleteItem()
{
    _selected_path = "";
}


